﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using ZK.Common.Log4;

namespace ZK.Qymd_TransManage
{
    public partial class Login : Form
    {
        public static string name
        {
            get; set;
        }
        public static string Id
        {
            get; set;
        }
        public static string Psw
        {
            get; set;
        }
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            label1.Text = Common.AppConfigHelper.GetAppSettings("MKName");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            LogManage.WriteInfo("登陆");
            string id = txtUsername.Text;
            string Password = txtPassword.Text;
            if (id == "" || Password == "")
            {
                MessageBox.Show("请输入用户名或密码");
                return;
            }
            if (id == "admin" && Password == "zkkj666")
            {
                Register register = new Register();
                register.ShowDialog();
                //this.Hide();
                return;
            }
            XmlDocument document = new XmlDocument();
            document.Load("UserData.xml");
            XmlNode node = document.SelectSingleNode($"/users/user[@id = {id}]");
            if (node != null)
            {
                string psw = node.SelectSingleNode("password").InnerText;
                //MessageBox.Show(psw);
                if (Password == psw)
                {
                    name = node.SelectSingleNode("name").InnerText;
                    Id = id;
                    Psw = psw;
                    Main main = new Main();
                    main.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("密码错误");
                }
            }
            else if (node == null)
            {
                MessageBox.Show("用户不存在");
            }
        }

    }
}
